<route lang="yaml">
  meta:
    enabled: false
  </route>

<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'
import { reactive, ref } from 'vue'

defineOptions({ name: 'SharePermissionForm' })

interface SysUserVO {
  id: number
  name: string
  phone: string
  wechat: string
  password: string
  address: string
}

const userFormRef = ref<FormInstance>()
const userForm = reactive<SysUserVO>({
  id: null,
  name: '',
  phone: '',
  wechat: '',
  password: '',
})

function validatePass(rule: any, value: any, callback: any) {
  if (ruleForm.id == null && value === '') {
    callback(new Error('请输入登录密码'))
  }
  else {
    if (userForm.password !== '') {
      if (!userFormRef.value) { return }
      userFormRef.value.validateField('password')
    }
    callback()
  }
}
const regex = /^1[3-9]\d{9}$/

function validatePhone(rule: any, value: any, callback: any) {
  if (value === '') {
    callback(new Error('请输入手机号'))
  }
  else if (regex.test(userForm.phone)) {
    callback(new Error('手机号格式不正确'))
  }
  else {
    callback()
  }
}

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 15, message: '字符数在2~15个字符', trigger: 'blur' },
  ],
  phone: [
    { validator: validatePhone, trigger: 'blur' },
  ],
  password: [
    { validator: validatePass, trigger: 'blur' },
  ],
})
async function submitForm(formEl: FormInstance | undefined) {
  if (!formEl) { return }
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
    }
    else {
      console.log('error submit!', fields)
    }
  })
}

function resetForm(formEl: FormInstance | undefined) {
  if (!formEl) { return }
  formEl.resetFields()
}

// 分配权限表单相关
const defaultProps = {
  children: 'children',
  label: 'label',
}

const sharePermissionFormModal = ref(false)

interface MemnuVO {
  id: string
  label: string
  level: number
  checked: boolean
}

const treeData = ref<MemnuVO[]>([
  { id: 10001, label: '平台治理', level: 1, children: [
    { id: 10004, label: '系统管理', level: 2 },
  ] },
  { id: 10002, label: 'CMR系统', level: 1, children: [
    { id: 10003, label: '订单管理', level: 2 },
    { id: 10004, label: '客户管理', level: 2 },
  ] },
])

const currentOperate = ref<string>('add')

const treeTableRef = ref()

function submitHandle() {
  const data = treeTableRef.value.getCheckedKeys()
  window.console.log(data)
}

function cancelHandle() {}

function getCurrentKeyHandle(data) {
  window.console.log(data)
}

async function openSharePermissionForm(id: number, type: string) {
  window.console.log(id)
  sharePermissionFormModal.value = true
  currentOperate.value = type
}
defineExpose({ openSharePermissionForm }) // 提供 open 方法，用于打开弹窗
</script>

<template>
  <el-drawer v-model="sharePermissionFormModal" direction="rtl">
    <template #header>
      <h4>分配权限</h4>
    </template>
    <template #default>
      <div>
        <el-tree
          ref="treeTableRef"
          style="max-width: 600px"
          :data="treeData"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :props="defaultProps"
          @get-current-key="getCurrentKeyHandle"
        />
      </div>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="cancelHandle">
          取消
        </el-button>
        <el-button type="primary" @click="submitHandle">
          提交
        </el-button>
      </div>
    </template>
  </el-drawer>
</template>

<style scoped>
 .share-form {
  width: 700px;
 }
</style>
